/** Files includes */
#include "stm32_it.h"
#include "bsp_gpio.h"
#include "string.h"
#include "multi_button.h"
#include "StateMachine.h"

void NVIC_Configure(uint8_t ch, uint8_t pri, uint8_t sub)
{
    NVIC_InitTypeDef NVIC_InitStruct;

    NVIC_InitStruct.NVIC_IRQChannel = ch;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = pri;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = sub;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
}

void Interrupt_Init(void)
{
    NVIC_Configure(TIM1_BRK_TIM9_IRQn, 0, 0);
    TIM_ClearITPendingBit(TIM1, TIM_IT_Break);
    TIM_ITConfig(TIM1, TIM_IT_Break, ENABLE);

    // 配置TIM1中断优先级
    NVIC_Configure(TIM1_UP_TIM10_IRQn, 0, 1);
    // 使能TIM1的溢出中断
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
    TIM_ClearFlag(TIM1, TIM_FLAG_Update);
    // 启动TIM1定时器
    TIM_Cmd(TIM1, ENABLE);

    // 配置TIM8中断优先级
    NVIC_Configure(TIM8_UP_TIM13_IRQn, 0, 2);
    // 使能TIM8的溢出中断
    TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);
    TIM_ClearFlag(TIM8, TIM_FLAG_Update);
    // 启动TIM8定时器
    TIM_Cmd(TIM8, ENABLE);

    // 配置TIM3中断优先级
    NVIC_Configure(TIM3_IRQn, 1, 2);
    // 使能TIM3的溢出中断
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
    TIM_ClearFlag(TIM3, TIM_FLAG_Update);
    // 启动TIM3定时器
    TIM_Cmd(TIM3, ENABLE);

    // 配置TIM4中断优先级
    NVIC_Configure(TIM4_IRQn, 1, 3);
    // 使能TIM4的溢出中断
    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);
    // 启动TIM4定时器
    TIM_Cmd(TIM4, ENABLE);

    // 配置USART2中断优先级
    NVIC_Configure(USART2_IRQn, 2, 0);
    USART_ClearFlag(USART2, USART_FLAG_TC);
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 开启相关中断
}

void EXTI9_5_IRQHandler(void) {}

void EXTI15_10_IRQHandler(void) {}

void DMA2_Stream0_IRQHandler(void)
{
     if (DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0) != RESET) 
     {
        // Clear DMA transfer complete interrupt flag
        DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
    }
}

void TIM1_BRK_TIM9_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM1, TIM_IT_Break) != RESET)
    {
        TIM_ClearITPendingBit(TIM1, TIM_IT_Break);
    }
}
uint16_t _1s_ticks = 0;
void TIM1_UP_TIM10_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
    {
        _1s_ticks++;
        TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
    }
}

void TIM8_UP_TIM13_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM8, TIM_IT_Update);
    }
}

void TIM3_IRQHandler(void)
{
    static uint8_t key_5ms_ticks = 0;
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
    {
        if (++key_5ms_ticks >= 5)
        {
            key_5ms_ticks = 0;
            button_ticks();
        }
        
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    }
}

void TIM4_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
    }
}

void I2C1_IRQHandler(void) {}

void SPI1_IRQHandler(void) {}

void USART1_IRQHandler(void) {}

void USART2_IRQHandler(void)
{
    u8 res;
    /* USART in Receiver mode */
    if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
    {
        res = USART_ReceiveData(USART2);
        USART_ClearFlag(USART2, USART_FLAG_TC);
    }
}
/**
 * @}
 */

/**
 * @}
 */
